<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Grimoires Documentation - Implementation Design - GShell</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />


<style type="text/css" media="all">
	/* Default TWiki layout */
	@import url("./layout.css");
	/* Default TWiki style */
	@import url("./style.css");
	/* Custom overriding layout per web or per topic */
	@import url("%USERLAYOUTURL%");
	/* Custom overriding style per web or per topic */
	@import url("%USERSTYLEURL%");
	.twikiToc li {
		list-style-image:url('i_arrow_down.gif');
	}	        
	.twikiWebIndicator {
		background-color:#D0D0D0;
	}
</style>
<style type="text/css" media="all"></style>
<script type="text/javascript">
<!-- HIDE
	function initPage() { }
-->
</script>
</head>
<body class="twikiViewPage twikiPrintPage">
<div class="twikiMiddleContainer"><div class="twikiMain"><div class="twikiTopic">
<h1><a name="Implementaion_Design"> </a> Implementaion Design - GShell </h1>
<p />
<div class="twikiToc">
<ul>
<li> <a href="#Implementaion_Design">Implementaion Design - GShell</a>
<ul>
<li> <a href="#Overview">Overview</a>
</li>
<li> <a href="#The_structure_explained">Shell explained</a>
</li>
<li> <a href="#Saver_Loader_and_RDQLGenerator_v">Command explained</a>
</li>
</ul>
</li>
</ul>
</div>
<h2><a name="Overview"> </a> Overview </h2>
<p>The Grimoires Shell (GShell) is a simple command line client for the 
Grimoires Web Service registry. The GShell provides a shell environment to 
interact with Grimoires service, within which a group of commands can be invoked 
for users to publish/inquire business/service/wsdl/metadata. </p>
<p>In terms of implementation, GShell consists of a Shell class that implements 
the shell environment, and a group of Command classes, each of which implements 
a registry task, for instance, publishing a service. Below we explain the Shell 
class and Command classes respectively. </p>
<p />
<h2><a name="The_structure_explained"> </a> Shell explained </h2>
<p />
<table border="1" cellspacing="1" cellpadding="0">
</table>
<p>The Shell class has implemented the following functionalities:</p>
<ul>
	<li>The Shell class is responsible for sending and receiving SOAP messages 
	to/from the Grimoires service through the Grimoires client API.</li>
	<li>The Shell class has provided some built-in utilities:<ul>
		<li>help: display a help message.</li>
		<li>ls: list all available commands. As a convention, all classes in the 
		package whose names end with <font face="Verdana">&quot;Command&quot; are 
		commands. The Shell class will search for all commands and list them 
		automatically. </font></li>
		<li>run command_name: invoke a command. The Shell class uses Java 
		Reflection API to create an instance of the specified Command class, and 
		invokes its <font face="Verdana">&quot;run&quot; method.</font></li>
		<li>exit: exit GShell.</li>
		<li>In addition, the Shell class also supports &quot;environment variables&quot;. 
		When you see [$n]=VALUE (where n is an integer) in the output, it means 
		the a variable, with name n, is recorded in GShell and you may reuse 
		them by typing &quot;$n&quot; to denote VALUE.</li>
	</ul>
	</li>
</ul>
<p>The Shell class need not be touched to implement a new command.</p>
<p />
<p />
<h2><a name="Saver_Loader_and_RDQLGenerator_v"> </a>Command explained</h2>
<p>All commands extend from the Shell class in order to inherit the shell 
environment, for instance, the capability to communicate with the Grimoires 
service. </p>
<p>All commands implements the Command interface, which is quite simple:</p>
<pre>
public interface Command {
    public void run() throws Exception;
}
</pre>
<p>All commands should provide a nullary constructor, which will be used by the 
Shell class to create an instance. Then the shell class will invoke the run() 
method of the created instance to execute the command.</p>
<p>In order to be runnable outside the shell, a command should also provide a 
public main() method. A sample command looks like:</p>
<pre>
public class PublishBusinessCommand extends Shell implements Command {
    
    private String name;
    private String description;
    private BusinessDetail response;

    public PublishBusinessCommand(String name, String desc) {
	this.name = name;
        this.description = desc;
    }
    
    public PublishBusinessCommand() {
        
    }

    private void expect() throws Exception {
        out.println("==== Publish a business ====");
	out.println("Input the business name:");
        name = readAndParseLine();
        out.println("Input the business description:");
        description = readAndParseLine();
     }

    private void execute() throws Exception {
        Save_business request = new Save_business();
        request.setAuthInfo("AUTHINFO");
        BusinessEntity[] businessEntities = new BusinessEntity[1];
        businessEntities[0] = new BusinessEntity();
        businessEntities[0].setName(new Name[] {
	    new Name(name)
        });         
        businessEntities[0].setDescription(new Description[] {
            new Description(description)
        });
        
        request.setBusinessEntity(businessEntities);
        response = publishImpl.save_business(request);
    }

    private void present() throws Exception {
        String businessKey = response.getBusinessEntity(0).getBusinessKey();
        printEnv("Business key", businessKey);
    }
    
    public void run() throws Exception {
    	expect();
        execute();
        present();
    }
    
    public static void main(String[] args) throws Exception {
        if (args.length != 3) {
            help();
            System.exit(1);
        }
        
        setUpStubs(args[0]);
        PublishBusinessCommand cmd = new PublishBusinessCommand(args[1], args[2]);
        cmd.execute();
        cmd.present();
    }

    private static void help() {
        System.out.println("java PubishBusinessCommand Grimoires_URL business_name business_description");
    }
}
</pre>
</div>
</body></html>